//
//  ByCellMineLast.m
//  chequanbao_ios
//
//  Created by mc on 2017/10/30.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellMineLast.h"
#import "ByUIFriendsAndRelativesVC.h"       // 亲朋好友

@implementation ByCellMineLast
{
    UIView      *_bgView;
    
    UILabel     *_leftInfoLabel;
    UILabel     *_leftLabel;
    
    UIView      *_lineView;
    
    UIImageView *_rightImg;
    UILabel     *_rightLabel;
    
//    NSInteger   _person;
    NSInteger   _section;
}
+ (instancetype)cellWithTableView:(UITableView*)tableView indexPath:(NSInteger)section{
    
    static NSString *cellIdentifier = @"ByCellMineLast";
    ByCellMineLast *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier  indexPath:section];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  indexPath:(NSInteger)section{
//    _person = person;
    _section = section;
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)createViews{
    // 背景视图
    CGFloat x,y,width,height;
    x = 10;
    y = 0;
    width = SCREEN_WIDTH - 2*x;
    height = width/3;
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _bgView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.25];
    _bgView.layer.cornerRadius = 4.0;
    [self addSubview:_bgView];
    
    // 左侧背景
    x = 0;
    y = 0;
    width = (_bgView.width - 0.5)/2;
    height = _bgView.height;
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    leftView.tag = 10 + _section;
    leftView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:leftView];
    _bgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftAction:)];
    [leftView addGestureRecognizer:tap];
    
//    x = (_bgView.width/2 - 40)/2;
    x = 0;
    y = (_bgView.height - 20 - 40 - 10)/2;
//    width = height = 40;
    width  = leftView.width;
    height = 40;
    _leftInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [_bgView addSubview:_leftInfoLabel];
    _leftInfoLabel.adjustsFontSizeToFitWidth = YES;
    _leftInfoLabel.font = FONT(24);
    _leftInfoLabel.textColor = [UIColor whiteColor];
    _leftInfoLabel.textAlignment = NSTextAlignmentCenter;
    
    x = 0;
    y = _leftInfoLabel.bottom + 10;
    width = (_bgView.width - 0.5)/2;
    height = 20;
    _leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _leftLabel.textAlignment = NSTextAlignmentCenter;
    _leftLabel.font = FONT(14);
    _leftLabel.text = @"亲朋好友";
    _leftLabel.textColor = [UIColor whiteColor];
    [_bgView addSubview:_leftLabel];
    
    // 分割线
    x = _leftLabel.right;
    y = 5;
    width = 0.5;
    height = _bgView.height - 2*y;
    _lineView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    [_bgView addSubview:_lineView];
    
    // 右侧背景
    x = _lineView.right;
    y = 0;
    width = leftView.width;
    height = leftView.height;
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    rightView.userInteractionEnabled = YES;
    rightView.tag = 100 + _section;
    rightView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:rightView];
    
//    x = _lineView.right + CGRectGetMinX(_leftInfoLabel.frame);
    x = (_bgView.width/2 - 40)/2 + _lineView.right;
    y = CGRectGetMinY(_leftInfoLabel.frame);
    width = height = 40;
    _rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [_rightImg setImage:[UIImage imageNamed:@"mine_contact"]];
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightAction:)];
    [rightView addGestureRecognizer:rightTap];
    [_bgView addSubview:_rightImg];
    
    
    x = _lineView.right;
    y = _rightImg.bottom + 10;
    width = _leftLabel.width;
    height = 20;
    _rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _rightLabel.textAlignment = NSTextAlignmentCenter;
    _rightLabel.font = FONT(14);
    _rightLabel.text = @"联系我们";
    _rightLabel.textColor = [UIColor whiteColor];
    [_bgView addSubview:_rightLabel];
    
    
    
}
- (void)setPerson:(NSString *)person{
    _leftInfoLabel.text = person;

}
- (void)rightAction:(UITapGestureRecognizer *)tap{
    NSString *tel1 = [ByNetConfig getCustomerPhone];
    NSString *tel = [NSString stringWithFormat:@"telprompt://%@",tel1];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
        });
    });
}

- (void)leftAction:(UITapGestureRecognizer *)tap{
    if (!BY_USER) {
        ByUILoginVC *vc = [[ByUILoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self.viewController presentViewController:navi animated:YES completion:nil];
        return;
    }
    ByUIFriendsAndRelativesVC *vc = [[ByUIFriendsAndRelativesVC alloc] init];
    [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
}

@end
